/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$('document').ready(function() {
    if ($('#profile_password_form').length > 0) {
        $('#profile_update_btn').click(function() {
        	return submit();
        });
        $("#profile_newpassword").passStrength({
        	userid : "#profile_user_email"
        });
    }
});

// 检查各字段是否填写正确
function check_info() {
    var ifChecked = true;
    clean_all_error_message()
    
    if ( $('#profile_oldpassword').val() === "" ) {    	
        // 添加错误信息
        $('#profile_oldpassword').after('<span class=\'help-inline\'>请您输入原密码！</span>');
        // 添加error class，更新界面
        $('div.control-group:has(#profile_oldpassword)').addClass('error');
        
        ifChecked = false;
    }
    if ( $('#profile_newpassword').val() === "" ) {
        // 添加错误信息
        $('#profile_newpassword').after('<span class=\'help-inline\'>请您输入新密码！</span>');
        // 添加error class，更新界面
        $('div.control-group:has(#profile_newpassword)').addClass('error');
        
        ifChecked = false;
    }
    else if ($('#profile_newpassword').val() === $('#profile_oldpassword').val()) {
    	$('#profile_newpassword').after('<span class=\'help-inline\'>新旧密码不可相同！</span>');
        $('div.control-group:has(#profile_newpassword)').addClass('error');
        
        ifChecked = false;
    }
    else if ($('#profile_newpassword').val() !== $('#profile_newpassword_repeat').val()) {
		$('#profile_newpassword').after('<span class=\'help-inline\'>密码输入不一致！</span>');
        $('div.control-group:has(#profile_newpassword)').addClass('error');
        
        ifChecked = false;
    }
    if ( $('#profile_newpassword_repeat').val() === "" ) {
        $('#profile_newpassword_repeat').nextAll('span').remove();
        // 添加错误信息
        $('#profile_newpassword_repeat').after('<span class=\'help-inline\'>请您再次输入新密码！</span>');
        // 添加error class，更新界面
        $('div.control-group:has(#profile_newpassword_repeat)').addClass('error');
        
        ifChecked = false;
    }
    
    return ifChecked;
}

function submit() {
    if ( check_info() ) {
    	var salt = genSalt();
        var pw_encrypt = encrypt(encrypt($('#profile_newpassword').val()) + salt);
        $.post("profile/update_password", {
            oldpassword: encrypt($('#profile_oldpassword').val()),
            newpassword: pw_encrypt,
            newsalt: salt
        }, function(data, textStatus) {
            if(data.code == -1) {
                location.href = "profile/show_profile_password";
            }
            else if(data.code == -2) {
            	clean_all_error_message();
                // 添加错误信息
                $('#profile_oldpassword').after('<span class=\'help-inline\'>原密码输入错误！</span>');
                // 添加error class，更新界面
                $('div.control-group:has(#profile_oldpassword)').addClass('error');
            }
            else if(data.code == 1) {
            	clean_all_error_message();
                $('#message_show').after('<span class=\'help-inline\'>密码更新成功</span>');
            }
        }, "json");
    }
    return false;
}

function clean_all_error_message() {
	//每次新的提交前都清除所有错误信息
	$('#profile_oldpassword').nextAll('span').remove();
	$('#profile_newpassword').nextAll('span').remove();
	$('#profile_newpassword_repeat').nextAll('span').remove();            	
    $('#message_show').nextAll('span').remove();
    $('.error').removeClass('error');
}
